package ro.msg.quiz.repository;

import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;

import javax.swing.JOptionPane;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * 
 * @author Adorjan This class is repository of quizzes.
 */
@XmlRootElement(name = "quizzes")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class RepositoryQuiz {
	private ArrayList<Quiz> quizes;
	private final String dataFormatString = "dd.MM.yyyy";

	/**
	 * Constructor
	 */
	public RepositoryQuiz() {
		quizes = new ArrayList<Quiz>();
	}

	/**
	 * Constructor
	 */
	public RepositoryQuiz(ArrayList<Quiz> quizList) {
		this.setQuizes(quizList);
		// readFromFileQuiz();
	}

	/**
	 * @return Returns the list of quizzes.
	 */
	@XmlElement(name = "quiz")
	public ArrayList<Quiz> getQuizes() {
		return quizes;
	}

	/**
	 * Sets the list of quizzes.
	 * 
	 * @param quizes
	 *            The list of quizzes to be set.
	 */
	public void setQuizes(ArrayList<Quiz> quizes) {
		this.quizes = quizes;
	}

	/**
	 * This method appends a quiz to repository.
	 * 
	 * @param quiz
	 *            The quiz to be appended to repository.
	 */
	public void addQuiz(Quiz quiz) {
		quizes.add(quiz);
	}

	/**
	 * This method removes a quiz from repository
	 * 
	 * @param quiz
	 *            The quiz to be removed from repository.
	 */
	public void removeQuiz(Quiz quiz) {
		quizes.remove(quiz);
	}

	/**
	 * @param id
	 *            The id of quiz.
	 * @return quiz Returns the quiz by id if it exists or null if fail.
	 */
	public Quiz getQuiz(int id) {
		for (Quiz quiz : quizes) {
			if (quiz.getId() == id)
				return quiz;
		}
		return null;
	}

	/**
	 * This method removes a quiz from repository by id.
	 * 
	 * @param id
	 *            The id of quiz.
	 */
	public void removeQuizById(int id) {
		for (Quiz quiz : quizes) {
			if (quiz.getId() == id) {
				removeQuiz(quiz);
				break;
			}
		}
	}

	/**
	 * This method edits a quiz.
	 * 
	 * @param id
	 *            The id of quiz.
	 * @param quizName
	 *            The name of quiz.
	 */
	public void editQuiz(int id, String quizName) {
		for (Quiz quiz : quizes) {
			if (quiz.getId() == id) {
				quiz.setQuizName(quizName);

				DateFormat dateFormat = new SimpleDateFormat(dataFormatString);
				Date date = new Date();
				quiz.setLastEdited(dateFormat.format(date));
			}
		}
	}

	/**
	 * Returns the iterator of quiz.
	 * 
	 * @return quizIterator
	 */
	public Iterator<Quiz> QuizIterator() {
		return quizes.iterator();
	}

	/**
	 * This method reads from text file.
	 */
	public void readFromFileQuiz(String path) {
		Scanner sc = null;
		try {
			sc = new Scanner(new FileReader(path + "\\quiz.txt"));
			String line = "";
			int id = 0;
			String name = "";
			while (sc.hasNextLine()) {
				line = sc.nextLine();
				String[] attr = line.split("[ ]");
				id = Integer.parseInt(attr[0]);
				name = attr[1];
				Quiz quiz = new Quiz(id, name);
				this.quizes.add(quiz);
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.toString(), "Error - QuizIn",
					JOptionPane.WARNING_MESSAGE);
		}
	}

	/**
	 * This method writes to text file.
	 */
	public void writeToFileQuiz(String path) {
		BufferedWriter bufferedWriter = null;
		try {
			bufferedWriter = new BufferedWriter(new FileWriter(path
					+ "\\quiz.txt"));
			for (int i = 0; i < quizes.size(); i++) {
				Quiz q = quizes.get(i);
				bufferedWriter.write(Integer.toString(q.getId()) + " "
						+ q.getQuizName() + " " + q.getLastEdited());
				bufferedWriter.newLine();
			}
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null, e.toString(),
					"Error - QuizOut", JOptionPane.WARNING_MESSAGE);
		} finally {
			try {
				bufferedWriter.close();
			} catch (Exception e) {
				JOptionPane.showMessageDialog(null, e.toString(),
						"Error - QuizOut", JOptionPane.WARNING_MESSAGE);
			}
		}
	}
}
